package com.lora.dao.impl;

import com.alibaba.fastjson.JSONObject;
import com.lora.dao.RegisterDao;
import com.lora.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@Slf4j
@Repository
public class RegisterDaoImpl implements RegisterDao {
    @Resource
    private JdbcTemplate jdbcTemplate;

    public int save(Map<String,Object> map){
        int i=jdbcTemplate.update("INSERT INTO register ( `name`, `explain`, `remarks`,`creattime`) VALUES ( ?, ?, ?,?)", map.get("name"),map.get("explain"),map.get("remarks"),map.get("creattime"));
        return i;
    }
    public List<Map<String,Object>> queryList(Map<String,Object> map){
        String name=(String)map.get("name");
        String explain=(String)map.get("explain");
        String order=(String)map.get("order");
        String sqlwhere="";
        String orders="";
        if(!StringUtils.isBlank(name)){
            sqlwhere+=" and name like '%"+name+"' ";
        }
        if(!StringUtils.isBlank(explain)){
            sqlwhere+=" and name explain '%"+explain+"' ";
        }
        if(!StringUtils.isBlank(order)){
            orders=" order by "+order+" desc ";
        }
        String sql="SELECT name as ename,id,`explain`,remarks,creattime   FROM `register` where 1=1 "+sqlwhere+orders+page(map);
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
        return maps;
    }
    public List<Map<String,Object>> accessList(Map<String,Object> map){
        String sql="SELECT*from entrance_guard where DATE_FORMAT(create_time,'%Y-%m-%d')=DATE_FORMAT(NOW(),'%Y-%m-%d') GROUP BY `name` ";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
        log.info("sql---:"+ JSONObject.toJSONString(maps));
        return maps;
    }
    public String page(Map<String,Object> map){
        String page=(String) map.get("page");
        String size=(String) map.get("rows");
        int p=1;
        int s=10;
        if(!StringUtils.isBlank(page)&&!page.equals("0")){
            p=Integer.valueOf(page);
        }
        if(!StringUtils.isBlank(page)&&!size.equals("0")){
            s=Integer.valueOf(size);
        }
        String a=" limit "+(p*s-s)+","+(p*s);
        return a;
    }
}
